//
//  Speed.swift
//  RapidPDF
//
//  Created by 周波 on 2024/9/29.
//

import SwiftUI

struct Speed: View {
    @Environment(\.presentationMode) var presentationMode
    @State private var isHighlighted: Bool = false
    @State private var progress: Double = 0
    @State private var timer: Timer?
    @State var isClick = true
    @State var isDo = true

    @ObservedObject var viewModel = SpeedViewModel()
    var body: some View {
        NavigationView {
            VStack(spacing: 0) {
                Back(title: "Speed Test", onBackTap: {
                    self.presentationMode.wrappedValue.dismiss()
                })
        
                HStack {
                    HStack{
                        
                        Text("Download")
                            .frame(width: 130)
                            .foregroundColor(.white)
                            .padding(.all, 10)
                            .background(!isHighlighted ? Color("coco") : Color("spbg"))
                            .cornerRadius(10)
                            .onTapGesture {
                                isHighlighted = false
                                viewModel.cancelTest()
                                viewModel.startTest(isDownload: true)
                                progress = viewModel.downloadSpeed
                            }
                        
                        Spacer()
                        
                        Text("Upload")
                            .frame(width:130)
                            .foregroundColor(.white)
                            .padding(.all, 10)
                            .background(!isHighlighted ? Color("spbg") : Color("coco"))
                            .cornerRadius(10)
                            .onTapGesture {
                                isHighlighted = true
                                viewModel.cancelTest()   // 取消之前的测试
                                viewModel.startTest(isDownload: false) // 开始上传测试
                                progress = viewModel.uploadSpeed
                            }
                        
                    }.onAppear{
                        
                        
                    }
                } .padding(.all,10)
                
                    .background(Color("spbg"))
                    .cornerRadius(20)
                    .padding(.top,20)
                    .padding(.horizontal,10)
                
                ZStack{
                 
                    CircularArcView(progress: isHighlighted ? $viewModel.uploadSpeed : $viewModel.downloadSpeed)

                    VStack(spacing:0){
                        Image(isHighlighted ? "dos" : "ups")
                        
                        Text(" \(isHighlighted ? viewModel.uploadSpeed   : viewModel.downloadSpeed, specifier: "%.2f")")
                            .font(.title)
                           .fontWeight(/*@START_MENU_TOKEN@*/.bold/*@END_MENU_TOKEN@*/)
                            .foregroundStyle(Color("coco"))
                        
                        Text("Mbs")
                           
                            .foregroundStyle(Color("coco"))
                        
                    
                    }
                   
                }.padding(.top,40)
                Spacer()
            }.onAppear{
                viewModel.cancelTest()
                viewModel.startTest(isDownload: true)
                progress = viewModel.downloadSpeed
            }
            .navigationViewStyle(StackNavigationViewStyle())
                .navigationBarHidden(true)
                .navigationBarBackButtonHidden(true)
        }.navigationViewStyle(StackNavigationViewStyle())
            .navigationBarHidden(true)
            .navigationBarBackButtonHidden(true)
    }
}


#Preview {
    Speed()
}
